package com.ed.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ed.context.UserContext;
import com.ed.domain.Option;
import com.ed.domain.Response;
import com.ed.domain.ResponseDetails;
import com.ed.dto.PageQueryDto;
import com.ed.mapper.IResponseDetailsMapper;
import com.ed.mapper.IResponseMapper;
import com.ed.result.PageResult;
import com.ed.service.ResponseService;
import com.ed.vo.ResponseQAVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ResponseServiceImpl extends ServiceImpl<IResponseMapper, Response> implements ResponseService {
    @Autowired
    private  IResponseMapper iResponseMapper;
    @Autowired
    private IResponseDetailsMapper iResponseDetailsMapper;
    @Override
    public PageResult myResponseList(PageQueryDto dto) {
        // 1.准备分页条件
        int pageNo = dto.getPageNo();
        int pageSize = dto.getPageSize();
        Page<Response> page = Page.of(pageNo, pageSize);
        // 2.排序条件 - 按照时间降序排列
        page.addOrder(new OrderItem().setColumn("submit_time").setAsc(false));
        // 2.根据当前用户id查询
        QueryWrapper<Response> queryWrapper = new QueryWrapper<Response>()
                .eq("user_id", UserContext.getCurrentUser());
        // 3.分页查询
        Page<Response> p = page(page, queryWrapper);
        // 4.解析
        long total = p.getTotal();
        List<Response> records = page.getRecords();
        log.info("rec:{}",records);
        return new PageResult(total, records);
    }

    @Override
    public List<ResponseQAVo> detailsByResponse(Response response) {
        ResponseDetails responseDetails = new ResponseDetails();
        List<ResponseQAVo> responseQAVoList = new ArrayList<>();
        Integer responseId = response.getId();   //记录id
        //1.多表关联，封装题目和选项
        // 使用Lambda表达式，更安全，编译时检查
        LambdaQueryWrapper<ResponseDetails> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.select(ResponseDetails::getQuestion,ResponseDetails::getOption,
                                ResponseDetails::getUserAnswer,ResponseDetails::getScore)
                .eq(ResponseDetails::getResponseId, responseId);

        List<ResponseDetails> responseDetailsList = iResponseDetailsMapper.selectList(lambdaQueryWrapper);

//        List<Option> options = responseDetailsList.stream()
//                .map(ops -> JSONUtil.toList(ops.getOption(), Option.class))
//                .flatMap(List::stream)
//                .collect(Collectors.toList());

        for (ResponseDetails details : responseDetailsList) {
            ResponseQAVo responseQAVo = new ResponseQAVo();

            responseQAVo.setTitle(details.getQuestion());
            responseQAVo.setType(1);
            responseQAVo.setAnswer(details.getUserAnswer());

            List<Option> options = JSONUtil.toList(details.getOption(), Option.class);
            for (Option option : options) {
                if(option.getName().equals(details.getUserAnswer())) {
                    responseQAVo.setChoice(option.getWeight());
                }
            }

            responseQAVo.setOptions(options);
            responseQAVo.setScore(details.getScore());

            responseQAVoList.add(responseQAVo);
        }
        log.info("responseQAVoList:{}",responseQAVoList);
        return responseQAVoList;
    }
}
